package com.wn.class30.service.impl;

import com.wn.class30.entity.Business;
import com.wn.class30.entity.BusinessExample;
import com.wn.class30.mapper.BusinessDao;
import com.wn.class30.service.BusinessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private BusinessDao businessDao;

    @Override
    public List<Business> selectBusiness(Map<String, String> map) throws Exception {
        return businessDao.selectByExample(getBusinessExample(map));
    }

    @Override
    public Long selectBusinessCount(Map<String, String> map) throws Exception {
        return businessDao.countByExample(getBusinessExample(map));
    }

    @Override
    public Business selectBusinessById(Integer id) throws Exception {
        return businessDao.selectByPrimaryKey(id);
    }

    @Override
    public void updateBusinessById(Business business) throws Exception {
        businessDao.updateByPrimaryKey(business);
    }

    @Override
    public void deleteBusinessById(Integer id) throws Exception {
        businessDao.deleteByPrimaryKey(id);
    }

    @Override
    public void insertBusiness(Business business) throws Exception {
        businessDao.insert(business);
    }

    private BusinessExample getBusinessExample(Map<String,String> map){
        BusinessExample businessExample = new BusinessExample();
        BusinessExample.Criteria criteria = businessExample.createCriteria();
        Long page = Long.parseLong(map.get("page"));
        Integer limit = Integer.parseInt(map.get("limit"));
        businessExample.setOffset(page);
        businessExample.setLimit(limit);
        return businessExample;
    }
}
